#@head() 
#@header()
<title>营销管理</title>
#@aside("marketing") 
<script type="text/javascript" src="#(path)/assets/js/jquery.tmpl.min.js"></script>
<div class="content-wrapper">
	<section class="content">
		<div class="nav-tabs-custom">
            <ul class="nav nav-tabs">
              <li class="active"><a href="#tab_1" data-toggle="tab" aria-expanded="true">优惠券管理</a></li>
              <!--<li class="active"><a href="#tab_2" data-toggle="tab" aria-expanded="false">还款管理</a></li>-->
            </ul>
            <div class="tab-content">
              <div class="tab-pane active" id="tab_1">
				 <button type="button" onclick="location.href='#(path)/admin/page/marketing-addred'" class="btn btn-block btn-info" style="width:120px;float:left;margin-top:10px;margin-right:20px;">新建优惠券</button>
				 <div class="input-group margin" style="width:400px;float:left">
					<input id="redname" type="text" class="form-control" placeholder="优惠券名称" >
					<span class="input-group-btn">
					  <button id="searchred" type="button" class="btn btn-info btn-flat">搜索</button>
					</span>
				</div>
				<div class="box-body">
					<table id="example1" class="table table-striped">
						<thead>
							<tr>
								<th>ID</th>
								<th>名称</th>
								<th>发放数量</th>
								<th>已领取</th>
								<th>
									<select id="status" class="">
										<option value="" >全部类型</option>
										<option value="tzhb">返现券</option>
										<option value="tyj">体验金</option>
										<option value="jxq">加息券</option>
									 </select>
								</th>
								<th>有效期</th>
								<th>结束时间</th>
								<th>状态</th>
								<th>操作</th>
							</tr>
						</thead>
						<tbody id="redlist">
						</tbody>
					</table>
				</div>
				<div class="box-footer clearfix">
				  <ul class="pagination pagination-sm no-margin pull-right">
					<li id="redlist1"><a href="#">首页</a></li>
					<li id="redlist2"><a href="#">上一页</a></li>
					<li><a href="#">第<span id="redlist5"></span>页 共<span id="redlist6"></span>页</a></li>
					<li id="redlist3"><a href="#">下一页</a></li>
					<li id="redlist4"><a href="#">尾页</a></li>
				  </ul>
				</div>
              </div>
            </div>
            <!-- /.tab-content -->
          </div>
		  
	</section>
</div>
#@footer()
<script id="redlistall" type="text/x-jquery-tmpl">
	<tr>
		<td>${id}</td>
		<td>${name}</td>
		<td>${num}<span> ${danwei}</span></td>
		<td>${apply_num}</td>
		<td>${type}</td>
		<td>${validtime}</td>
		<td>${end_time}</td>
		<td>${status}</td>
		<td>
			<div class="btn-group">
			 <button onclick="location.href='#(path)/admin/page/marketing-receivered?id=${id}'" type="button" class="btn btn-info">领取详情</button>
			<button pass="${id}" type="button" class="btn bg-yellow pass">手动结束</button>
			<button onclick="location.href='#(path)/admin/page/marketing-marketinfo?id=${id}'" type="button" class="btn btn-success">详情</button>
			</div>
		</td>
	</tr>
</script>
<script>
var totalPage = 1;
var pageNumber = 1;
var type = "";
$(document).ready(function(){
	pageNumber = 1;
	$("#redlist").empty();
	redlist(1,"",type);
	$("#redlist5").html(pageNumber);
});
$("#redlist1").click(function(){
	$("#redlist").empty();
	redlist(1,"",type);
	pageNumber = 1;
	$("#redlist5").html(pageNumber);
});
$("#status").change(function(){
	type = $("#status").val();
	pageNumber = 1;
	$("#redlist").empty();
	redlist(1,"",type);
	$("#redlist5").html(pageNumber);
});
$("#redlist2").click(function(){
	if(pageNumber==1){
		alert("当前页是首页!");
	}else{
	$("#redlist").empty();
	pageNumber = pageNumber-1;
	redlist(pageNumber,"",type);
	$("#redlist5").html(pageNumber);
	}
});
$("#redlist3").click(function(){
	if(pageNumber==totalPage){
		alert("当前页是尾页!");
	}else{
	$("#redlist").empty();
	pageNumber = pageNumber +1;
	redlist(pageNumber,"",type);
	$("#redlist5").html(pageNumber);
	}
});
$("#redlist4").click(function(){
	$("#redlist").empty();
	redlist(totalPage,"",type);
	pageNumber = totalPage;
	$("#redlist5").html(pageNumber);
});
$("#searchred").click(function(){
	$("#redlist").empty();
	var redname = $("#redname").val();
	redlist(1,redname,type);
	pageNumber = totalPage;
	$("#redlist5").html(pageNumber);
});
function redlist(page,name,type){
	if(!$.isBlank(name)){
		name = "~"+name;
	}
	$.getJSON("/coupon/list?token=#(session.token??)&type="+type+"&page="+page+"&size=12&name="+name, function(redli) {
		var danwei = "张";
		totalPage = redli.data.page.totalPage;
		$("#redlist6").html(totalPage);
		$.each(redli.data.page.list, function(i, item){ 
			if(item.type=="tzhb"){
				var type="返现券";
			}
			if(item.type=="jxq"){
				var type="加息券";
			}
			if(item.type=="tyj"){
				var type="体验金";
			}
			if(item.status=="enable"){
				var status="启用";
			}else if(item.status=="disable"){
				var status="停用";
			}else if(item.status=="auto"){
				var status="自动结束";
			}else if(item.status=="stop"){
				var status="手动结束";
			}else if(item.status=="finish"){
				var status="发放完毕";
			}
			if(item.valid_day == 0){
				
				var validtime = item.valid_start_time + "——" +item.valid_end_time;
			}else {
				var validtime = "自领取"+ item.valid_day + "天内";
			}
			var redlist = {apply_num:item.apply_num+"张",end_time:item.end_time,danwei:danwei,validtime:validtime,id:item.id,name:item.name,num:item.num,type:type,status:status};
			$("#redlistall").tmpl(redlist).appendTo('#redlist');
		});
		$(".pass").click(function(){
			var idval = $(this).attr("pass");
			 
			$.getJSON("/coupon/stop?token=#(session.token??)&id="+idval, function(data) {
				if(data.code=='success'){
					layer.msg(data.msg, {icon:1,time:1000},function(){
						location.href="#(path)/admin/page/marketing-redpacket"
					});
				}else if(data.code=='error'){
					layer.msg(data.msg, {icon:2,time:1000});
				}
			});
		});
	});	
}
</script>
</body>
</html>

